package com.accenture.fd.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.springframework.stereotype.Service;

import com.accenture.fd.antlr.java.Java8Lexer;
import com.accenture.fd.antlr.java.Java8Parser;
import com.accenture.fd.dto.ExportExcelItemDto;
import com.accenture.fd.dto.ExportExcelResultDto;
import com.accenture.fd.service.ExportDtoExcelService;

@Service
public class ExportDtoExcelServiceImpl implements ExportDtoExcelService {

	private int sortnum = 0;

	@Override
	public ExportExcelResultDto createDtoExcel(String content) {

		ExportExcelResultDto dto = new ExportExcelResultDto();

		CharStream input = null;
		try {
			input = CharStreams.fromFileName(content);
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		Java8Lexer lexer = new Java8Lexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		Java8Parser parser = new Java8Parser(tokens);
		ParserRuleContext ctx = parser.compilationUnit();

		List<ExportExcelItemDto> list = new ArrayList<>();

		this.generateAST(ctx, false, 0, dto, list);
		dto.setExportExcelItemDtoList(list);
		sortnum = 0;
		return dto;
	}

	private void generateAST(RuleContext ctx, boolean verbose, int indentation, ExportExcelResultDto dto,
			List<ExportExcelItemDto> list) {
		boolean toBeIgnored = !verbose && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext;

		if (!toBeIgnored) {
			String ruleName = Java8Parser.ruleNames[ctx.getRuleIndex()];
			//获取类doc
			if (ruleName.equals("typeDeclaration")) {
				String doc = ctx.getChild(0).getText();
				doc = doc.replace("*", "").replace("/","").replace(" ", "").replace("\r\n","");
				if(doc.contains("@")) {
					int indexa = doc.indexOf("@");
					doc = doc.substring(0, indexa);
				}
				dto.setClassDoc(doc);
			}
			//类名
			if (ruleName.equals("normalClassDeclaration")) {
				dto.setClassName(ctx.getChild(2).getText());
			}
			//获取包名
			if (ruleName.equals("packageDeclaration")) {
				dto.setPackageName(ctx.getChild(1).getText());
			}
			//获取属性
			if (ruleName.equals("fieldDeclaration")) {
				if (list.size() != 0) {
					int num = sortnum - 1;
					list.get(num).setItemType(ctx.getChild(1).getText());
					list.get(num).setItemName(ctx.getChild(2).getText());
				}

			}
			//项目注释
			if (ruleName.equals("classMemberDeclaration")) {
				ExportExcelItemDto itemDto = new ExportExcelItemDto();
				String doc = ctx.getChild(0).getText();
				doc = doc.replace("*", "").replace("/","").replace(" ", "");
				itemDto.setItemDoc(doc);
				itemDto.setSortKey(sortnum + 1);
				sortnum++;
				list.add(itemDto);
			}
		}
		for (int i = 0; i < ctx.getChildCount(); i++) {
			ParseTree element = ctx.getChild(i);
			if (element instanceof RuleContext) {
				generateAST((RuleContext) element, verbose, indentation + (toBeIgnored ? 0 : 1), dto, list);
			}
		}

	}

}
